/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Vue;

import Modele.Album;
import Modele.ModeleImage;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

/**
 *
 * @author Soufiane GARCH
 */
public class VueMiniature extends JPanel{
    
    private VueImageMiniature vIM;
    private JLabel label;
    private ModeleImage mImg;
    private VuePrincipale vuePrincipale;
    private Album albumActuel;
    private JMenuItem supImg;
    private final JPopupMenu popup;
    
    
    public VueMiniature(ModeleImage mI, VuePrincipale vP){
        this.vuePrincipale = vP;
        this.albumActuel = this.vuePrincipale.getBiblio().getAlbumActuel();
        this.mImg = mI;
        this.label = new JLabel(mI.getNom());
        vIM = new VueImageMiniature(mI, vP);
        this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
        this.label.setAlignmentX(Component.CENTER_ALIGNMENT);
        this.vIM.setAlignmentX(Component.CENTER_ALIGNMENT);
        
        this.add(label);
        this.add(vIM);
        this.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        
        popup = new JPopupMenu();
        supImg = new JMenuItem("Supprimer de l'album");
        popup.add(supImg);
        supImg.addActionListener(new ActionListener() {
            @SuppressWarnings("static-access")
            public void actionPerformed(ActionEvent e) {
                albumActuel = vuePrincipale.getBiblio().getAlbumActuel();
                
                if (albumActuel == null){
                        JOptionPane d = new JOptionPane();
                        d.showMessageDialog(vuePrincipale,"Aucun Album selectionné","Supprimer un album",JOptionPane.WARNING_MESSAGE);
                        return;
                }
            
                albumActuel.supprimerImage(mImg);
                vuePrincipale.afficheMiniature(albumActuel);
                vuePrincipale.setImageActuel(null);
                JOptionPane d = new JOptionPane();
                d.showMessageDialog(vuePrincipale,"L'image \"" + mImg.toString() + "\" a été supprimé avec succé.","Supprimer un répertoire",JOptionPane.WARNING_MESSAGE);
            
            }
        });
        
        final VueMiniature myObject = this;
        
        addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                popup.show(myObject, e.getX(), e.getY());
                }
            }
        }); 
    
    }
    
    /*
    @Override
    public void repaint(){
        vIM.repaint();
        //super.repaint();
    }*/
    
    public String getNom(){
        return mImg.getNom();
    }
    
    
    
    class MyAdapt extends MouseAdapter
    {
        private JPanel panel;
        public MyAdapt(JPanel panel){
            this.panel = panel;
        }
        // Interception du click
        @Override
        public void mouseClicked(MouseEvent e){
           
            if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1){
                vuePrincipale.afficheImage(mImg);
            }
        }

    } 
    
    public void recadrer(boolean b){
        if (b == true) this.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        else this.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        
    }
    
    /*    
    public static void main(String args[]) {
        ModeleImage mi1 = new ModeleImage("C:\\Documents and Settings\\garch0011\\Bureau\\test\\Automne.jpg");
        ModeleImage mi2 = new ModeleImage("C:\\Documents and Settings\\garch0011\\Bureau\\test\\Cristal.jpg");
        ModeleImage mi3 = new ModeleImage("C:\\Documents and Settings\\garch0011\\Bureau\\test\\Floraison.jpg");
        ModeleImage mi4 = new ModeleImage("C:\\Documents and Settings\\garch0011\\Bureau\\test\\Sommet.jpg");
        
        VueMiniature img1 = new VueMiniature(mi1);
        VueMiniature img2 = new VueMiniature(mi2);
        VueMiniature img3 = new VueMiniature(mi3);
        VueMiniature img4 = new VueMiniature(mi4);
        
        JFrame fe = new JFrame();
        Container cont = fe.getContentPane();
        JPanel pan = new JPanel();
        pan.setLayout(new FlowLayout());
        pan.add(img1);
        pan.add(img2);
        pan.add(img3);
        pan.add(img4);
        cont.add(pan);
        fe.setSize(800, 600);
        fe.setEnabled(true);
        fe.setVisible(true);
    }*/
}
